/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.component;

import java.awt.Font;
import java.util.Enumeration;

import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.plaf.FontUIResource;

/**
 *
 * @author knight
 *  语音识别
 */
public class Speech_Recognition extends javax.swing.JFrame {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/** Creates new form Speech_Recognition */
    public Speech_Recognition(NotifyObject notifyObject) {
    	this.notifyObject = notifyObject;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {

        showHistory = new javax.swing.JScrollPane();
        operationPanel = new javax.swing.JPanel();
        startButton = new javax.swing.JButton();
        endButton = new javax.swing.JButton();
        showPanel = new javax.swing.JPanel();
        showLabel = new javax.swing.JLabel();
        history = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        tipBar = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        wordPane = new javax.swing.JTextPane();

        showHistory.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Noto Sans Mono CJK SC Regular", 0, 12))); // NOI18N
        showHistory.setAutoscrolls(true);
        showHistory.setFont(new java.awt.Font("Noto Sans Mono CJK SC Regular", 0, 12)); // NOI18N
        showHistory.setMaximumSize(new java.awt.Dimension(100, 200));
        showHistory.setName("showHistory"); // NOI18N

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        operationPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("操作"));

        startButton.setBackground(new java.awt.Color(25, 163, 213));
        startButton.setForeground(new java.awt.Color(13, 21, 28));
        startButton.setText("start");
        startButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                startClick(evt);
            }
        });

        endButton.setBackground(new java.awt.Color(25, 163, 213));
        endButton.setForeground(new java.awt.Color(13, 21, 28));
        endButton.setText("end");
        endButton.setEnabled(false);
        endButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                endClick(evt);
            }
        });

        org.jdesktop.layout.GroupLayout operationPanelLayout = new org.jdesktop.layout.GroupLayout(operationPanel);
        operationPanel.setLayout(operationPanelLayout);
        operationPanelLayout.setHorizontalGroup(
            operationPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(operationPanelLayout.createSequentialGroup()
                .add(32, 32, 32)
                .add(startButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 121, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(endButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 114, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(31, 31, 31))
        );
        operationPanelLayout.setVerticalGroup(
            operationPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(operationPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(operationPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(startButton)
                    .add(endButton))
                .addContainerGap(33, Short.MAX_VALUE))
        );

        showPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("语音识别"));
        showPanel.setToolTipText("");
        showPanel.setName(""); // NOI18N

        showLabel.setFont(new java.awt.Font("Noto Sans Mono CJK SC Regular", 0, 12)); // NOI18N
        showLabel.setVerticalAlignment(javax.swing.SwingConstants.TOP);

        history.setBackground(new java.awt.Color(181, 236, 101));
        history.setFont(new java.awt.Font("Noto Sans Mono CJK SC Regular", 0, 12)); // NOI18N
        history.setText("history");
        history.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                historyMouseClicked(evt);
            }
        });

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jTextArea1.setEnabled(false);
        jScrollPane2.setViewportView(jTextArea1);

        org.jdesktop.layout.GroupLayout showPanelLayout = new org.jdesktop.layout.GroupLayout(showPanel);
        showPanel.setLayout(showPanelLayout);
        showPanelLayout.setHorizontalGroup(
            showPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(showLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 666, Short.MAX_VALUE)
            .add(showPanelLayout.createSequentialGroup()
                .addContainerGap(598, Short.MAX_VALUE)
                .add(history)
                .addContainerGap())
            .add(jScrollPane2)
        );
        showPanelLayout.setVerticalGroup(
            showPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(showPanelLayout.createSequentialGroup()
                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 224, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(history)
                .add(51, 51, 51)
                .add(showLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 106, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        tipBar.setFont(new java.awt.Font("Noto Sans CJK SC", 0, 14)); // NOI18N
        tipBar.setForeground(new java.awt.Color(207, 173, 42));
        tipBar.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        tipBar.setText("welcome to robot !");
        tipBar.setToolTipText("");
        tipBar.setBorder(javax.swing.BorderFactory.createTitledBorder("提示"));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("分词结果"));

        jScrollPane1.setViewportView(wordPane);

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jScrollPane1)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE)
                .addContainerGap())
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(operationPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .add(showPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .add(tipBar, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(operationPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(tipBar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 72, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(showPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 291, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void startClick(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_startClick
    	
        this.startButton.setBackground(new java.awt.Color(213, 57, 25));
        this.startButton.setEnabled(false);
        this.endButton.setEnabled(true);
        this.history.setEnabled(false);
        //invoke speechrecognition
        notifyObject.setjTextArea1(jTextArea1);
        notifyObject.notifyRecognizeStart();

    }//GEN-LAST:event_startClick

    private void endClick(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_endClick
        this.startButton.setBackground(new java.awt.Color(25, 163, 213));
        this.startButton.setEnabled(true);
        this.endButton.setEnabled(false);
        this.history.setEnabled(true);
        //invoke speechrecognition
        notifyObject.notifyRecognizeEnd();
    }//GEN-LAST:event_endClick
    
    private void historyMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_historyMouseClicked
        //历史信息
    	String result = notifyObject.readLog(10);
        JOptionPane.showMessageDialog(showHistory, result,"history",JOptionPane.PLAIN_MESSAGE);
    }//GEN-LAST:event_historyMouseClicked
    
    public static void setUIFont() {
	  FontUIResource fontRes = new FontUIResource(new Font("微软雅黑",Font.PLAIN,14)); 
      for (Enumeration<Object> keys = UIManager.getDefaults().keys();keys.hasMoreElements();) { 
  	        Object key = keys.nextElement(); 
  	        Object value = UIManager.get(key); 
  	        if (value instanceof FontUIResource) { 
  	        	UIManager.put(key, fontRes); 
  	        } 
      	}
    }
    
    private NotifyObject notifyObject;
    
    private String tip = "welcome to robot!";
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton endButton;
    private javax.swing.JButton history;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JPanel operationPanel;
    private javax.swing.JScrollPane showHistory;
    private javax.swing.JLabel showLabel;
    private javax.swing.JPanel showPanel;
    private javax.swing.JButton startButton;
    private javax.swing.JLabel tipBar;
    private javax.swing.JTextPane wordPane;
    // End of variables declaration//GEN-END:variables

}
